package com.kinggm.service.impl;

import com.kinggm.annotation.DataSourceName;
import com.kinggm.mapper.DogMapper;
import com.kinggm.model.Dog;
import com.kinggm.service.DogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class DogServiceImpl implements DogService {

    @Resource
    private DogMapper dogMapper;


    // 自己注入自己
    @Resource
    private DogService dogService;


    // 为了让加了事务的方法也能成功切换数据源
    // 必须在事务开启之前就切换数据源   这种处理方式真的很奇葩
    // 而且这又涉及到 事务的失效问题 同一个类中必须使用代理对象调用事务方法事务才能生效
    @DataSourceName("dataSourceTwo")
    public int savePre(Dog dog){
       return dogService.save(dog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(Dog dog) {
        dogMapper.insert(dog);
        int a = 1/0;
        return dog.getId();
    }

}
